package so.ifl.manager.admin;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.web.DoradoContext;

import so.ifl.commons.tools.DoradoSystemUtils;
import so.ifl.servcie.ExamBookingDetailService;
import so.ifl.vo.AccountVo;
import so.ifl.vo.ExamBookingDetailVo;

/**
 * 
 * create by simon create at 2012-1-18 上午9:25:25 <mailto:liangyuanliu@gmail.com>
 */
@Component("exambookingdetailManager")
public class ExamBookingDetailManager {

	@Autowired
	private ExamBookingDetailService exambookingdetailService;
	private Logger logger_ = LoggerFactory.getLogger(this.getClass());

	/**
	 * 
	 * simon create at 2012-1-18 上午9:25:21 <mailto:liangyuanliu@gmail.com>
	 * 
	 * @param page
	 * @param map
	 */
	@DataProvider
	public void getExamBookingDetail(Page<ExamBookingDetailVo> page, Map<String, Object> map) {
		
		logger_.debug("params:" + map);

		int offset = page.getFirstEntityIndex();
		int limmit = page.getPageSize();
		List<ExamBookingDetailVo> list = exambookingdetailService.queryExamBookingDetailWithParamAndPagination(map, offset, limmit);
		int countsql = exambookingdetailService.countExamBookingDetailWithParam(map);
		page.setEntities(list);
		page.setEntityCount(countsql);
	}

	@DataResolver
	public void updateExamBookingDetail(Collection<ExamBookingDetailVo> ExamBookingdetails) {
		for (ExamBookingDetailVo exambookingdetail : ExamBookingdetails) {
			EntityState entityState = EntityUtils.getState(exambookingdetail);
			if (EntityState.NEW.equals(entityState)) {
				// 新增
				insertExamBookingDetail(exambookingdetail);
			} else if (EntityState.MODIFIED.equals(entityState)) {
				// 修改
				updateExamBookingDetail(exambookingdetail);
			} else if (EntityState.DELETED.equals(entityState)) {
				// 删除
				deleteExamBookingDetail(exambookingdetail.getBookingDetailId());
			}
		}
	}

	/**
	 * 新增考试预约明细 simon create at 2012-1-18 上午9:25:14
	 * <mailto:liangyuanliu@gmail.com>
	 * 
	 * @param exambookingdetailVo
	 */
	private void insertExamBookingDetail(ExamBookingDetailVo exambookingdetailVo) {
		//Map<String, Object> m = utilMap(exambookingdetailVo);
		exambookingdetailService.insertExamBookingDetail(exambookingdetailVo);
	}

	/**
	 * 更新考试预约明细 simon create at 2012-1-18 上午9:25:06
	 * <mailto:liangyuanliu@gmail.com>
	 * 
	 * @param exambookingdetailVo
	 */
	private void updateExamBookingDetail(ExamBookingDetailVo exambookingdetailVo) {
		Map<String, Object> m = utilMap(exambookingdetailVo);
		m.put("booking_detail_id_", exambookingdetailVo.getBookingDetailId());
		exambookingdetailService.updateExamBookingDetail(m);
	}

	/**
	 * 删除考试预约明细 simon create at 2012-1-18 上午9:24:54
	 * <mailto:liangyuanliu@gmail.com>
	 * 
	 * @param id
	 */
	private void deleteExamBookingDetail(int id) {
		exambookingdetailService.deleteExamBookingDetail(id);
	}

	/**
	 * simon create at 2012-1-18 上午9:24:44 <mailto:liangyuanliu@gmail.com>
	 * 
	 * @param exambookingdetailVo
	 * @return
	 */
	private Map<String, Object> utilMap(ExamBookingDetailVo exambookingdetailVo) {
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("exam_booking_id_", exambookingdetailVo.getExamBookingId());
		m.put("student_id_", exambookingdetailVo.getStudentId());
		m.put("student_name_", exambookingdetailVo.getStudentName());
		m.put("student_certificate_number_", exambookingdetailVo.getStudentCertificateNumber());
		m.put("student_file_id_", exambookingdetailVo.getStudentFileId());
		m.put("get_cert_time_", exambookingdetailVo.getGetCertTime());
		m.put("test_type_", exambookingdetailVo.getTestType());
		m.put("exam_result_", exambookingdetailVo.getExamResult());
		m.put("detail_note_", exambookingdetailVo.getDetailNote());
		m.put("student_phone_", exambookingdetailVo.getStudentPhone());
		m.put("student_sex_", exambookingdetailVo.getStudentSex());
		m.put("test_car_type_", exambookingdetailVo.getTestCarType());
		m.put("test_play_", exambookingdetailVo.getTestPlay());
		m.put("tests_address_", exambookingdetailVo.getTestsAddress());
		m.put("company_abridge_", exambookingdetailVo.getCompanyAbridge());
		m.put("student_skill_date_", exambookingdetailVo.getStudentSkillDate());
		
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			m.put("account_login_name_", accountVo.getAccountLoginName());
			m.put("account_owner_", accountVo.getAccountOwner());
		}
		return m;
	}



}
